local TestLuaBehavior = class("TestLuaBehavior",require 'Comm/LuaBehavior')
local util = require 'xlua/util'

function TestLuaBehavior:Awake()
    print("TestLuaBehavior:Awake")
    --@type UnityEngine.SpriteRenderer
    local sp = self.gameObject:GetComponentInChildren(typeof(SpriteRenderer))
    sp.color = Color.red
    sp:DOFade(.2, 2):SetEase(DOTween_Ease.OutCubic):SetLoops(-1, DOTween_LoopType.Yoyo)

    local m = CS.AudioManager.Instance
    m:PlaySoundEffect("dd")

    --传入的
    self.light.uobject.gameObject:SetActive(false)

    --update 测试，需要通过handler包装以后，OnUpdate中的self才代表这个对象
    self.updateHandler = handler(self, self.OnUpdate)
    self.tick = 0;
    AddUpdateListener(self.updateHandler)
end

function TestLuaBehavior:Start()
    self.co = self:StartCoroutine(handler(self,self.CoroutineFunction));
end
function TestLuaBehavior:CoroutineFunction()
    logWarn('----Wait for 5 seconds')
    coroutine.yield(CS.UnityEngine.WaitForSeconds(5))
    logWarn('----Wait for 5 seconds end')
    coroutine.yield(CS.UnityEngine.WaitForEndOfFrame())
    logWarn('----Wait for End Frame')

    -- 停止和Destory都会关闭协程
    -- self:StopCoroutine(self.co)
    GameObject.Destroy(self.gameObject);
    logWarn('----Stop Coroutine')

    coroutine.yield(CS.UnityEngine.WaitForSeconds(3))
    logWarn('----Wait for 3 seconds end')
    coroutine.yield(CS.UnityEngine.WaitForFixedUpdate())
    logWarn('----Wait for Fixed Update')
end

function TestLuaBehavior:OnUpdate()
    print("update " .. self.gameObject.name)
    self.tick = self.tick + 1
    if self.tick>50 then
        RemoveUpdateListener(self.updateHandler)
        print("stop update "..self.gameObject.name)
    end
end

function TestLuaBehavior:OnDestroy()
    RemoveUpdateListener(self.updateHandler)
    print("TestLuaBehavior destroy")
end

return TestLuaBehavior
